<template>
  <div class="chat-container">
    <div class="chat-header">AI助手</div>

    <div class="chat-box" ref="chatBox">
      <div v-for="(msg, index) in messages" :key="index" :class="['chat-msg', msg.role]">
        <div class="bubble">
          <span class="role-label">{{ msg.role === 'user' ? '🧑‍💻 我' : '🤖 模型' }}</span>
          <div class="text" v-if="msg.role === 'user'">{{ msg.text }}</div>
          <div class="markdown-body" v-else v-html="msg.text"></div>
        </div>
      </div>
      <div v-if="loading" class="chat-msg bot">
        <div class="bubble">
          <span class="role-label">🤖 模型</span>
          <div class="text">
            正在思考<span class="dot"></span><span class="dot"></span><span class="dot"></span>
          </div>
        </div>
      </div>
    </div>

    <form class="input-area" @submit.prevent="handleSubmit">
      <input v-model="input" type="text" placeholder="请输入您的问题..." />
      <button type="submit" :disabled="loading">发送</button>
    </form>
  </div>
</template>

<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import { renderMarkdown } from '../utils/markdownParser'

const input = ref('')
// 用于存放所有的消息，包括用户的消息，机器人的消息
const messages = ref<{ role: 'user' | 'bot'; text: string }[]>([])
const chatBox = ref<HTMLElement | null>(null)
const loading = ref(false)

// 定义一个异步函数 handleSubmit，用于处理用户提交问题的逻辑
async function handleSubmit() {
  // 获取输入框中的内容并去除前后空格
  const question = input.value.trim()

  // 如果输入内容为空，则直接返回，不执行后续逻辑
  if (!question) return

  // 将用户输入的问题加入 messages 列表，用于渲染聊天记录
  messages.value.push({ role: 'user', text: question })

  // 清空输入框
  input.value = ''

  // 设置 loading 状态为 true，表示机器人正在“思考中”
  loading.value = true

  await nextTick() // 等待 DOM 的更新，将用户的问题先更新到页面上面
  // 进行一个滚动，滚动当前最下面的消息
  chatBox.value?.scrollTo({ top: chatBox.value.scrollHeight, behavior: 'smooth' })

  // 模拟API调用
  setTimeout(() => {
    loading.value = false
    const botMessage = {
      role: 'bot' as const,
      text: renderMarkdown(`感谢您的提问！这是一个模拟回复。\n\n您的问题是：${question}\n\n由于这是一个演示环境，我无法真正连接到后端API。在实际部署中，这里会发送请求到后端服务器获取真实的AI回复。`)
    }
    messages.value.push(botMessage)
    
    // 滚动到底部
    nextTick(() => {
      chatBox.value?.scrollTo({ top: chatBox.value.scrollHeight, behavior: 'smooth' })
    })
  }, 1000)
}
</script>

<style scoped>
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.chat-header {
  text-align: center;
  padding: 16px;
  font-size: 20px;
  font-weight: bold;
  border-bottom: 1px solid #e0e0e0;
}

.chat-box {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.chat-msg {
  display: flex;
  margin-bottom: 12px;
}

.chat-msg.user {
  justify-content: flex-end;
}

.chat-msg.bot {
  justify-content: flex-start;
}

.bubble {
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 18px;
  line-height: 1.4;
  position: relative;
}

.chat-msg.user .bubble {
  background: #daf1ff;
  color: #333;
  border-bottom-right-radius: 4px;
}

.chat-msg.bot .bubble {
  background: #e6e6e6;
  color: #222;
  border-bottom-left-radius: 4px;
}

.role-label {
  font-size: 12px;
  opacity: 0.7;
  display: block;
  margin-bottom: 4px;
}

.input-area {
  display: flex;
  padding: 12px;
  border-top: 1px solid #ddd;
  background: #fff;
}

.input-area input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid #ccc;
  border-radius: 20px;
  font-size: 16px;
  outline: none;
}

.input-area button {
  margin-left: 10px;
  padding: 10px 18px;
  font-size: 16px;
  border: none;
  border-radius: 20px;
  background-color: #007bff;
  color: white;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.input-area button:hover {
  background-color: #0056b3;
}

.input-area button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.dot {
  animation: blink 1s infinite;
}

.dot:nth-child(2) {
  animation-delay: 0.2s;
}

.dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes blink {
  0%,
  80%,
  100% {
    opacity: 0;
  }
  40% {
    opacity: 1;
  }
}

/* 代码块样式增强 */
.markdown-body pre {
  padding: 1rem;
  border-radius: 6px;
  background-color: #f6f8fa;
  overflow: auto;
}
</style>